package com.MC.MCard.dao;

import com.MC.MCard.entity.Merchant;

import java.sql.*;

public class MerchantDao {
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/MembershipCard/framework?characterEncoding=utf8&useSSL=false";
    private static final String USER = "root";
    private static final String PASSWORD = "Amin6248";

    public static Merchant findByEmail(String memail) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Merchant merchant = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            ps = conn.prepareStatement("SELECT M_ID,M_NAME,M_EMAIL,M_PSWD FROM merchant WHERE M_EMAIL=?");


            ps.setString(1,"memail");

            rs = ps.executeQuery();
            if (rs.next()) {
                merchant = new Merchant();
                merchant.setMid(rs.getInt("M_ID"));
                merchant.setMname(rs.getString("M_NAME"));
                merchant.setMmail(rs.getInt("M_PHONE"));
                merchant.setMpassword(rs.getString("M_PSWD"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }



        try {
            rs.close();
            ps.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return merchant;
    }

}
